/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.view;

import entity.JobStatusStatement;
import entity.Jobs;
import entity.Message;
import entity.Users;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import session.JobStatusFacade;
import session.JobStatusStatementFacade;
import session.JobsFacade;
import session.MessageFacade;

/**
 *
 * @author owtee
 */
public class ApplicationMsgApprove extends HttpServlet {

    @EJB
    private MessageFacade messageFacade;
    @EJB
    private JobStatusStatementFacade jobStatusStatementFacade;
    @EJB
    private JobStatusFacade jobStatusFacade;
    @EJB
    private JobsFacade jobsFacade;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            try {
                String appId = request.getParameter("appId");
                boolean ddlEmploy = (request.getParameter("ddlEmploy").equals("true") ? true : false);
                String txtMsgRemark = request.getParameter("txtMsgRemark").replace("<br/>", "\n");
                String express = request.getParameter("express");

                // Get session
                HttpSession session = request.getSession();
                Users member = (Users) session.getAttribute("member");

                Jobs job = jobsFacade.find(appId);
                job.setMsgUser(member);
                job.setMsgActionDate(new Date());
                job.setEmployMsgFlag(ddlEmploy);
                if (express.equals("N")) {
                    job.setMsgRemark(txtMsgRemark);
                }
                job.setStatusId(jobStatusFacade.find("04"));
                job.setModifyUser(member.getUserId());
                job.setModifyDate(new Date());
                jobsFacade.edit(job);

                // Create job status statement
                JobStatusStatement jobStatusStatement = new JobStatusStatement();
                jobStatusStatement.setJobId(job);
                jobStatusStatement.setStatusId(jobStatusFacade.find("04"));
                jobStatusStatement.setCreateUser(member.getUserId());
                jobStatusStatement.setCreateDate(new Date());
                jobStatusStatement.setModifyUser(member.getUserId());
                jobStatusStatement.setModifyDate(new Date());
                jobStatusStatementFacade.create(jobStatusStatement);

                Message message = messageFacade.find("104");
                request.setAttribute("message", message);

            } catch (Exception ex) {
                // Found Error
                Message message = messageFacade.find("029");
                String m = message.getMesDetail() + " : " + ex.getMessage();
                message.setMesDetail(m);
                request.setAttribute("message", message);
            }

            RequestDispatcher rd = request.getRequestDispatcher("ApplicationMsgGetAll");
            rd.forward(request, response);

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
